/******************************************************************************
 * Name: X86TraceManager.h
 * Author: ZhengJuyu, zhengjuyu@gmail.com
 * Date: 2008/08/24
 * Revision history:
 *
 * Purpose:
 *     Do the basic optimization "trace linking" on basic block, on IA32
 * architecture 
 *
 *****************************************************************************/

#ifndef X86TRACEMANAGER_H
#define X86TRACEMANAGER_H

#include "crossbit/TraceManager.h"
#include <map>
#include <vector>
using std::map;
using std::vector;
namespace crossbit
{
    class X86TraceManager : public TraceManager
    {
        public:
            X86TraceManager( MemoryManager *mm );
            virtual ~X86TraceManager();

            virtual void directLinkTo( TBlock* preBlock, XTInt32 exit, TBlock* block);
            virtual void callLinkTo( TBlock* preBlock, XTInt32 exit, TBlock* block);
            virtual void indirectLinkTo( TBlock* preBlock, XTInt32 exit, TBlock* block);
            virtual void linkSuperBlock( TBlock* former_tblock , TBlock* super_tblock); 

        private:
            virtual void fillPredictionCodeBlock( TBlock* block );

            virtual XTMemAddr allocatePredictionCodeBlock();


        private:

            /******************************************************************
             * On IA32 arichtecture, the prediction code block contain 3 
             * instructions:
             *      lea   -0x40001000(%edx), %ecx    ; 6 bytes
             *      jecxz match                      ; 2 bytes
             *      jmp	  nextPrediction             ; 6 bytes
             *
             * align it to 16 bytes
             ******************************************************************/
            // 72 bytes is allocated , for 5 comparison-branch, one mov i -> ebx, one je hashtable_dispatch
            static const XTInt32 PREDICTIONCODEBLOCKLEN = 72; 

            map<XTMemAddr , vector<XTMemAddr> *> link_table;

            XTInt8 sample[PREDICTIONCODEBLOCKLEN];
    };
}
#endif	//X86TRACEMANAGER_H
